package com.study;

public class LinkedList<E> implements LinearList<E> {
    private static class Node<E> {
        E item;
        Node<E> next;

        Node(E item, Node<E> next) {
            this.item = item;
            this.next = next;
        }
    }

    private int size;
    private Node<E> start;

    public LinkedList() {
        start = null;
    }

    @Override
    public boolean add(E item) {
        if (start == null) {
            start = new Node<E>(item, null);
        } else {
            Node<E> current = start;
            while (current.next != null) {
                current = current.next;
            }
            current.next = new Node<E>(item, null);

        }
        size++;
        return true;
    }

    @Override
    public boolean add(int i, E item) {
        Node<E> current;
        Node<E> previous;
        if (i < 0 || i > size) {
            return false;
        }
        Node<E> newnode = new Node<E>(item, null);
        if (i == 0) {
            newnode.next = start;
            start = newnode;
            size++;
        } else {
            current = start;
            previous = null;
            int j = 0;
            if (current != null && j < i) {
                previous = current;
                current = current.next;
                j++;
            }
            if (j == i) {
                previous.next = newnode;
                newnode.next = current;
                size++;
            }
        }
        return true;
    }
// 似乎有错
    @Override
    public E remove(int i) {
        E oldValue = null;
        if (isEmpty() || i < 0 || i > size - 1) {
            oldValue = null;
            return oldValue;
        }
        Node<E> current = start;
        Node<E> previous = null;
        if (i == 0) {
            oldValue = current.item;
            start = current.next;
            current = null;
            size--;
        } else {
            int j = 0;
            while (current != null && j < i) {
                previous = current;
                current = current.next;
                j++;
            }
            if (j == i) {
                previous.next = current.next;
                oldValue = current.item;
                current = null;
                size--;
            }
        }
        return oldValue;

    }

    @Override
    public int indexOf(E item) {
        int i = 0;
        if (item == null) {
            for (Node<E> current = start; current != null; current = current.next) {
                if (current.item == null) {
                    return i;
                }
                i++;
            }
        } else {
            for (Node<E> current = start; current != null; current = current.next) {
                if (item.equals(current.item)) {
                    return i;
                }
                i++;
            }
        }
        return -1;
    }

    @Override
    public E get(int i) {
        E item = null;
        int j = 0;
        if (isEmpty() || i < 0 || i > size - 1) {
            item = null;
        }
        Node<E> current= start;
        while(current.next!=null && j<i){
            current =current.next;
            j++;
        }
        if(j==i){
            item=current.item;
        }
        return item;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void clear() {
        for(Node<E> current=start;current!=null;){
            Node<E> next =current.next;
            current.next=null;
            current.item=null;
            current=next;
        }
        start =null;
        size= 0;
    }

    @Override
    public boolean isEmpty() {
        return size ==0;
    }
}
